﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;

/// <summary>
/// SchoolCommon 的摘要说明
/// </summary>
public class SchoolCommon
{
    public SchoolCommon()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //


    }
    /// <summary>
    /// 获取评价周期
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static string GetEvalPeriod(int num)
    {
        if (num <= 0 || num > 5) return "";
        string[] OptStrArr = { "每天", "每周", "每月", "每学期", "每学年" };

        return OptStrArr[num - 1];
    }
    public static SchoolInfo Sch_SchoolInfo
    {
        get
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", Common.GetConstSchoolMark());
            //查询学校信息表，获取该校学期值
            IList<SchoolInfo> listSchoolInfo = Container<ISchoolInfoLogic>.Instance.FindAll(ht);
            return listSchoolInfo.Count == 0 ? null : listSchoolInfo[0];
        }
    }

    /// <summary>
    /// 操作权限验证
    /// </summary>
    /// <param name="Securityid">权限ID</param>
    /// <param name="SecurityValue">权限值</param>
    /// <param name="year">权限ID</param>
    /// <param name="term">权限值</param>
    /// <returns>是否拥有此权限</returns>
    public static bool OperatorSecurityCheck( int Securityid, int SecurityValue,int year,int term)
    {
        Hashtable ht = new Hashtable();
        //ht.Add("ValueMax",0);
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecurityValue);
        ht.Add("SchoolYear", year);
        ht.Add("Term", term);
        int value = Container<ISecuritySettingLogic>.Instance.GetOperatorSecurityValue(ht);
        if (value>0)
        {
            return true;
        }
        
        return false;
    }

    private static bool OperatorSecurityCheck(Hashtable ht)
    {
        int value = Container<ISecuritySettingLogic>.Instance.GetOperatorSecurityValue(ht);
        if (value > 0)
        {
            return true;
        }

        return false;
    }
    //public static int GetViewSecurityValue(int Securityid, int year, int term)
    //{
    //    Hashtable ht = new Hashtable();
    //    ht.Add("ValueMax", 3);
    //    ht.Add("TeacherID", Common.GetUserInfo().UID);
    //    ht.Add("SecurityID", Securityid);
    //    ht.Add("SchoolYear", year);
    //    ht.Add("Term", term);
    //    int value = Container<ISecuritySettingLogic>.Instance.GetViewSecurityValue(ht);

    //    return value;
    //}
    ///// <summary>
    ///// 获取可以查看的教师ID列表
    ///// </summary>
    ///// <param name="schoolyear"></param>
    ///// <param name="term"></param>
    ///// <returns>null:查看全部,不为NULL:可查看的所有列表</returns>
    //public static List<int> CanViewTeacher(int Securityid, int schoolyear, int term)
    //{
    //    List<int> tids = new List<int>();
    //    int viewv = GetViewSecurityValue(Securityid, schoolyear,term);
    //    switch(viewv )
    //    {
    //        case 3:
    //        case 4:
    //        case 5:
    //        case 6:
    //        case 7:
    //            break;
    //        case 8:
    //            return null;
    //            break;
    //    }
    //    return null;
    //}

    /// <summary>
    /// 获取可以查看的班级ID列表
    /// </summary>
    /// <param name="schoolyear"></param>
    /// <param name="term"></param>
    /// <returns>null:查看全部,不为NULL:可查看的所有列表</returns>
    public static List<int> CanViewClasses(int Securityid, int schoolyear, int term)
    {
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        List<int> cids = new List<int>();
        //1 判断是否有浏览全校权限,有返回null
        Hashtable ht = new Hashtable();
        //ht.Add("ValueMax",0);
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MySchool_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            return null;
        }
        //2 判断是否有本年级浏览权限,有返回当时该年级的所有班
        ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGrade_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            ht = new Hashtable();
            ht.Add("TeacherID", Common.GetUserInfo().UID);
            ht.Add("SecurityID", Securityid);
            ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGrade_Security);
            ht.Add("SchoolYear", schoolyear);
            ht.Add("Term", term);
            ht.Add("SchoolMark", si.SchoolMark);
            IList<TeacherRoles> sss = Container<ISecuritySettingLogic>.Instance.FindTeacherRoles(ht);
           
            string strgids = "-1";
            foreach (TeacherRoles s in sss)
            {
                strgids += ","+s.GreadID;
            }

            ht = new Hashtable();
            ht.Add("Grades", strgids);
            ht.Add("SchoolYear", schoolyear);
            ht.Add("SchoolMark", si.SchoolMark);
            //ht.Add("IsEffect",1);
            IList<ClsInfo> css = Container<ISecuritySettingLogic>.Instance.FindClsinfo(ht);
            foreach(ClsInfo c in css)
            {
                if (!cids.Contains(c.RID))
                {
                    cids.Add(c.RID);
                }
            }
            //return cids;
        }
        //3 判断是否有本班浏览权限,当时自己所在的班
        ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyClass_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            //班主任
            ht = new Hashtable();
            ht.Add("SchoolYear", schoolyear);
            ht.Add("SchoolMark", si.SchoolMark);
            ht.Add("HeaderID", Common.GetUserInfo().UID);
            //ht.Add("IsEffect",1);
            IList<ClsInfo> css = Container<ISecuritySettingLogic>.Instance.FindClsinfo(ht);
            foreach(ClsInfo c in css)
            {
                if (!cids.Contains(c.RID))
                {
                    cids.Add(c.RID);
                }
            }

            //任课教师
            ht = new Hashtable();
            ht.Add("SchoolYear", schoolyear);
            ht.Add("SchoolMark", si.SchoolMark);
            ht.Add("TeacherID", Common.GetUserInfo().UID);
            IList<TeachClsCause> tss = Container<ISecuritySettingLogic>.Instance.FindTeachClsCause(ht);
            foreach (TeachClsCause c in tss)
            {
                if (!cids.Contains(c.ClsID))
                {
                    cids.Add(c.ClsID);
                }
            }
        }
        //4 都没有,返回cids
                ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyCasue_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            return null;
        }
        return cids;
    }

    /// <summary>
    /// 获取可以查看的年级ID列表
    /// </summary>
    /// <param name="schoolyear"></param>
    /// <param name="term"></param>
    /// <returns>null:查看全部,不为NULL:可查看的所有列表</returns>
    public static List<int> CanViewGrade(int Securityid, int schoolyear, int term)
    {
        List<int> gids = new List<int>();
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        //1 判断是否有浏览全校权限,有返回null
        Hashtable ht = new Hashtable();
        //ht.Add("ValueMax",0);
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MySchool_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            return null;
        }
        //2 判断是否有本年级浏览权限,有返回自己当时所在年级
        ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGrade_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            ht = new Hashtable();
            ht.Add("TeacherID", Common.GetUserInfo().UID);
            ht.Add("SecurityID", Securityid);
            ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGrade_Security);
            ht.Add("SchoolYear", schoolyear);
            ht.Add("Term", term);
            ht.Add("SchoolMark", si.SchoolMark);
            IList<TeacherRoles> sss = Container<ISecuritySettingLogic>.Instance.FindTeacherRoles(ht);

            
            foreach (TeacherRoles s in sss)
            {
                if (s.GreadID!=0 && !gids.Contains(s.GreadID))
                {
                    gids.Add(s.GreadID);
                }
            }

            //return cids;
        }
        //3 都没有,返回gids
        return gids;
    }

    /// <summary>
    /// 获取可以查看的教研组ID列表
    /// </summary>
    /// <param name="schoolyear"></param>
    /// <param name="term"></param>
    /// <returns>null:查看全部,不为NULL:可查看的所有列表</returns>
    public static List<int> CanViewSubjects(int Securityid, int schoolyear, int term)
    {
        List<int> sids = new List<int>();
        //1 判断是否有浏览全校权限,有返回null
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        Hashtable ht = new Hashtable();
        //ht.Add("ValueMax",0);
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MySchool_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            return null;
        }
        //2 判断是否有本浏览本教研组权限,有返回自己当时所在教研组
        ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyCasue_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            ht = new Hashtable();
            ht.Add("TeacherID", Common.GetUserInfo().UID);
            ht.Add("SecurityID", Securityid);
            ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyCasue_Security);
            ht.Add("SchoolYear", schoolyear);
            ht.Add("Term", term);
            ht.Add("SchoolMark", si.SchoolMark);
            IList<TeacherRoles> sss = Container<ISecuritySettingLogic>.Instance.FindTeacherRoles(ht);
            foreach (TeacherRoles s in sss)
            {
                if (s.CauseID != 0 && !sids.Contains(s.CauseID))
                {
                    sids.Add(s.CauseID);
                }
            }
        }

        //3 判断是否有本年级浏览权限,有返回自己当时所在年级
        ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGrade_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            return null;
        }
        //4 都没有返回sids

        return sids;
    }

    /// <summary>
    /// 获取可以查看的备课组列表
    /// </summary>
    /// <param name="schoolyear"></param>
    /// <param name="term"></param>
    /// <returns>null:查看全部,不为NULL:可查看的所有列表</returns>
    public static List<GradeCause> CanViewGradeSubjects(int Securityid, int schoolyear, int term)
    {
        List<GradeCause> sids = new List<GradeCause>();
        //1 判断是否有浏览全校权限,有返回null
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        Hashtable ht = new Hashtable();
        //ht.Add("ValueMax",0);
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MySchool_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            return null;
        }
        //2 判断是否有浏览本教研组权限,有则返回本教研组
        ht = new Hashtable();
        ht.Add("TeacherID", Common.GetUserInfo().UID);
        ht.Add("SecurityID", Securityid);
        ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGradeCasue_Security);
        ht.Add("SchoolYear", schoolyear);
        ht.Add("Term", term);
        if (OperatorSecurityCheck(ht))
        {
            ht = new Hashtable();
            ht.Add("TeacherID", Common.GetUserInfo().UID);
            ht.Add("SecurityID", Securityid);
            ht.Add("SecurityValue", SecuritySetting.SecurityValues.Show_MyGradeCasue_Security);
            ht.Add("SchoolYear", schoolyear);
            ht.Add("Term", term);
            ht.Add("SchoolMark", si.SchoolMark);
            IList<TeacherRoles> sss = Container<ISecuritySettingLogic>.Instance.FindTeacherRoles(ht);
            foreach (TeacherRoles s in sss)
            {
                if (s.CauseID != 0 && s.GreadID != 0 && !CheckGradeCause(sids,s.CauseID,s.GreadID))
                {
                    GradeCause g = new GradeCause();
                    g.CauseID = s.CauseID;
                    g.Grade = s.GreadID;
                    sids.Add(g);
                }
            }
        }
        //3 都没有返回sids
        return sids;
    }
    private static bool CheckGradeCause(List<GradeCause> sids,int causeid,int grade)
    {
        foreach (GradeCause s in sids)
        {
            if (s.CauseID == causeid && s.Grade == grade)
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 取得指定学期的指定周的日期范围
    /// </summary>
    /// <returns></returns>
    public static bool GetWeekInTerm(int schoolYear, int term, String schoolMark, int numberOfWeek, out DateTime startDay, out DateTime endDay)
    {
        startDay = new DateTime();
        endDay = new DateTime();

        Hashtable ht = new  Hashtable();
        ht.Add("SchoolYear", schoolYear);
        ht.Add("Term", term);
        ht.Add("SchoolMark", schoolMark);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if(infoList == null || infoList.Count < 1)
            return false;

        TermInfo info = infoList[0];
        int dayOfWeek = (int)((DateTime)info.StartDate).DayOfWeek;
        if (dayOfWeek == 0)
            dayOfWeek = 7;
        startDay = ((DateTime)info.StartDate).AddDays((numberOfWeek - 1) * 7).AddDays(-dayOfWeek+1);
        endDay = startDay.AddDays(7);

        return true;
    }

    /// <summary>
    /// 取得指定学期的指定月的周数目
    /// </summary>
    /// <param name="schoolYear">学年(年)</param>
    /// <param name="term">学期</param>
    /// <param name="year">目标年</param>
    /// <param name="month">目标月</param>
    /// <param name="startWeek">输出: 本学期的第几周</param>
    /// <param name="endWeek">输出: 本学期的第几周</param>
    /// <returns></returns>
    public static bool GetWeekInMonth(int schoolYear, int term, String schoolMark, int year, int month, out int startWeek, out int endWeek)
    {
        startWeek = 0;
        endWeek = 0;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", schoolYear);
        ht.Add("Term", term);
        ht.Add("SchoolMark", schoolMark);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return false;

        TermInfo info = infoList[0];
        int dayOfWeek = (int)((DateTime)info.StartDate).DayOfWeek;
        if (dayOfWeek == 0)
            dayOfWeek = 7;
        DateTime dtStart = new DateTime(year, month, 1);
        DateTime dt2 = new DateTime(((DateTime)info.StartDate).Year, ((DateTime)info.StartDate).Month, ((DateTime)info.StartDate).Day);
        TimeSpan s = new TimeSpan(dtStart.Ticks - dt2.Ticks);
        startWeek = (s.Days - dayOfWeek + 1) / 7;


        DateTime dtEnd = dtStart.AddMonths(1).AddDays(-1);
        //DateTime dtEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        s = new TimeSpan(dtEnd.Ticks - dt2.Ticks);
        endWeek = (s.Days - dayOfWeek + 1) / 7;

        return true;
    }

    /// <summary>
    /// 获取指定日期在学期中处于第几周
    /// </summary>
    /// <param name="schoolYear">学年</param>
    /// <param name="term">学期</param>
    /// <param name="dateNow">指定日期</param>
    /// <param name="numOfWeek">输出: 处于指定学期第几周</param>
    /// <returns></returns>
    public static bool GetWeekOfTerm(int schoolYear, int term, String schoolMark, DateTime dateNow, out int numOfWeek)
    {
        numOfWeek = 0;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", schoolYear);
        ht.Add("Term", term);
        ht.Add("SchoolMark", schoolMark);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return false;

        TermInfo info = infoList[0];
        int dayOfWeek = (int)((DateTime)info.StartDate).DayOfWeek;
        if (dayOfWeek == 0)
            dayOfWeek = 7;

        DateTime dt1 = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day);
        DateTime dt2 = new DateTime(((DateTime)info.StartDate).Year, ((DateTime)info.StartDate).Month, ((DateTime)info.StartDate).Day);

        TimeSpan s = new TimeSpan(dt1.Ticks - dt2.Ticks);

        numOfWeek = s.Days / 7 + 1;

        return true;
    }

    public static void GetLastTerm(ref int schoolyear, ref int term)
    {
        if (term == 2)
        {
            //当前是上学期
            term = 1;
        }
        else if (term == 1)
        {
            schoolyear = schoolyear - 1;
            term = 2;
        }
    }
    public static void FirstMoral()
    {
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", si.SchoolYear);
        ht.Add("Term", si.SchoolTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IMoralityHistoryLogic eduUserLogic = Container<IMoralityHistoryLogic>.Instance;
        IList<MoralityHistory> stuhis = eduUserLogic.FindAll(ht);
        if (stuhis.Count > 0)
            return;
        int lastYear = si.SchoolYear;
        int lastTerm = si.SchoolTerm;
        GetLastTerm(ref lastYear, ref lastTerm);
        //去年的历史
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<MoralityHistory> lasthistory = Container<IMoralityHistoryLogic>.Instance.FindAll(ht);




        foreach (MoralityHistory mh in lasthistory)
        {
            IList<MoralityType> lasttype = null;
            IList<MoralityItem> lastitems = null;
            //填历史
            MoralityHistory seh = new MoralityHistory();
            seh.Attribute = mh.Attribute;
            seh.Caption = mh.Caption;
            seh.EvalType = mh.EvalType;
            seh.KeyWord = mh.KeyWord;
            seh.Site = mh.Site;
            seh.ManagerID = 0;
            seh.SchoolYear = si.SchoolYear;
            seh.Term = si.SchoolTerm;
            seh.Status = 0;
            seh.SchoolMark = si.SchoolMark;
            int newhistoryid = Container<IMoralityHistoryLogic>.Instance.Create(seh);

            if (lasttype == null)
            {
                ht = new Hashtable();
                ht.Add("HistoryID", mh.RID);
                ht.Add("SchoolMark", si.SchoolMark);
                lasttype = Container<IMoralityTypeLogic>.Instance.FindAll(ht);
            }
            if (lastitems == null)
            {
                ht = new Hashtable();
                ht.Add("HistoryID", mh.RID);
                ht.Add("SchoolMark", si.SchoolMark);
                lastitems = Container<IMoralityItemLogic>.Instance.FindAll(ht);
            }
            foreach (MoralityType t in lasttype)
            {
                int oldtypeid = t.RID;
                t.HistoryID = newhistoryid;
                t.SchoolMark = si.SchoolMark;
                //填类别
                int newtypeid = Container<IMoralityTypeLogic>.Instance.Create(t);
                foreach (MoralityItem i in lastitems)
                {
                    if (oldtypeid == i.TypeID)
                    {
                        i.TypeID = newtypeid;
                        i.HistoryID = newhistoryid;
                        i.SchoolMark = si.SchoolMark;
                        //填题目
                        Container<IMoralityItemLogic>.Instance.Create(i);
                    }
                }

            }
        }

        
    }
    public static bool Find(int id, IList<StuCauseEvalHistory> stuhis)
    {
        foreach (StuCauseEvalHistory s in stuhis)
        {
            if (s.CauseID == id)
            {
                return true;
            }
        }
        return false;
    }
    public static void FirstCredit()
    {

        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);

        IList<CauseInfo> cis = Container<ICauseInfoLogic>.Instance.FindAll(ht);

        ht = new Hashtable();
        ht.Add("SchoolYear", si.SchoolYear);
        ht.Add("Term", si.SchoolTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IStuCauseEvalHistoryLogic eduUserLogic = Container<IStuCauseEvalHistoryLogic>.Instance;
        IList<StuCauseEvalHistory> stuhis = eduUserLogic.FindAll(ht);
        if (stuhis.Count > 0)
            return;

        int lastYear = si.SchoolYear;
        int lastTerm = si.SchoolTerm;
        GetLastTerm(ref lastYear, ref lastTerm);
        //去年的历史
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<StuCauseEvalHistory> lasthistory = Container<IStuCauseEvalHistoryLogic>.Instance.FindAll(ht);
        IList<StuCauseEvalType> lasttype = null;
        IList<StuCauseEvalItem> lastitems = null;
        foreach (CauseInfo c in cis)
        {
            if (!Find(c.RID, stuhis))
            {
                int oldhistoryid = GetLastYearID1(c.RID, lasthistory);
                if (oldhistoryid != 0)
                {
                    //填历史
                    StuCauseEvalHistory seh = new StuCauseEvalHistory(0, si.SchoolYear, si.SchoolTerm, c.RID, 0, 0);
                    seh.SchoolMark = si.SchoolMark;
                    int newhistoryid = Container<IStuCauseEvalHistoryLogic>.Instance.Create(seh);

                    ht = new Hashtable();
                    ht.Add("HistoryID", oldhistoryid);
                    ht.Add("SchoolMark", si.SchoolMark);
                    lasttype = Container<IStuCauseEvalTypeLogic>.Instance.FindAll(ht);

                    ht = new Hashtable();
                    ht.Add("HistoryID", oldhistoryid);
                    ht.Add("SchoolMark", si.SchoolMark);
                    lastitems = Container<IStuCauseEvalItemLogic>.Instance.FindAll(ht); 
  
                    foreach (StuCauseEvalType t in lasttype)
                    {
                        if (t.CauseID == c.RID)
                        {
                            int oldtypeid = t.RID;
                            t.HistoryID = newhistoryid;
                            t.SchoolMark = si.SchoolMark;
                            //填类别
                            int newtypeid = Container<IStuCauseEvalTypeLogic>.Instance.Create(t);
                            foreach (StuCauseEvalItem i in lastitems)
                            {
                                if (t.CauseID == c.RID && oldtypeid == i.TypeID)
                                {
                                    i.TypeID = newtypeid;
                                    i.HistoryID = newhistoryid;
                                    i.SchoolMark = si.SchoolMark;
                                    //填题目
                                    Container<IStuCauseEvalItemLogic>.Instance.Create(i);
                                }
                            }
                        }
                    }
                }
                else
                {
                    StuCauseEvalHistory seh = new StuCauseEvalHistory(0, si.SchoolYear, si.SchoolTerm, c.RID, 0, 0);
                    seh.SchoolMark = si.SchoolMark;
                    int newhistoryid = Container<IStuCauseEvalHistoryLogic>.Instance.Create(seh);
                }
            }
        }

        //级别
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<StuCauseLevel> lastlevel = Container<IStuCauseLevelLogic>.Instance.FindAll(ht);
        foreach (StuCauseLevel scl in lastlevel)
        {
            scl.SchoolYear = si.SchoolYear;
            scl.Term = si.SchoolTerm;
            scl.SchoolMark = si.SchoolMark;
            Container<IStuCauseLevelLogic>.Instance.Create(scl);
        }
    }

    public static int GetLastYearID1(int causeid, IList<StuCauseEvalHistory> lasthistory)
    {
        foreach (StuCauseEvalHistory s in lasthistory)
        {
            if (s.CauseID == causeid)
                return s.RID;
        }
        return 0;
    }
    public static void FirstClassEval()
    {
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", si.SchoolYear);
        ht.Add("Term", si.SchoolTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IClassEvalHistoryLogic eduUserLogic = Container<IClassEvalHistoryLogic>.Instance;
        IList<ClassEvalHistory> stuhis = eduUserLogic.FindAll(ht);
        if (stuhis.Count > 0)
            return;
        int lastYear = si.SchoolYear;
        int lastTerm = si.SchoolTerm;
        GetLastTerm(ref lastYear, ref lastTerm);
        //去年的历史
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<ClassEvalHistory> lasthistory = Container<IClassEvalHistoryLogic>.Instance.FindAll(ht);



        foreach (ClassEvalHistory mh in lasthistory)
        {
            IList<ClassEvalType> lasttype = null;
            IList<ClassEvalItem> lastitems = null;
            //填历史
            ClassEvalHistory seh = new ClassEvalHistory();
            seh.Attribute = mh.Attribute;
            seh.Caption = mh.Caption;
            seh.EvalType = mh.EvalType;
            seh.KeyWord = mh.KeyWord;
            seh.Site = mh.Site;
            seh.ManagerID = 0;
            seh.SchoolYear = si.SchoolYear;
            seh.Term = si.SchoolTerm;
            seh.Status = 0;
            seh.SchoolMark = si.SchoolMark;
            int newhistoryid = Container<IClassEvalHistoryLogic>.Instance.Create(seh);

            if (lasttype == null)
            {
                ht = new Hashtable();
                ht.Add("HistoryID", mh.RID);
                ht.Add("SchoolMark", si.SchoolMark);
                lasttype = Container<IClassEvalTypeLogic>.Instance.FindAll(ht);
            }
            if (lastitems == null)
            {
                ht = new Hashtable();
                ht.Add("HistoryID", mh.RID);
                ht.Add("SchoolMark", si.SchoolMark);
                lastitems = Container<IClassEvalItemLogic>.Instance.FindAll(ht);
            }
            foreach (ClassEvalType t in lasttype)
            {
                int oldtypeid = t.RID;
                t.HistoryID = newhistoryid;
                t.SchoolMark = si.SchoolMark;
                //填类别
                int newtypeid = Container<IClassEvalTypeLogic>.Instance.Create(t);
                foreach (ClassEvalItem i in lastitems)
                {
                    if (oldtypeid == i.TypeID)
                    {
                        i.TypeID = newtypeid;
                        i.HistoryID = newhistoryid;
                        i.SchoolMark = si.SchoolMark;
                        //填题目
                        Container<IClassEvalItemLogic>.Instance.Create(i);
                    }
                }

            }
        }

        
    }

    public static void FirstTeacherEval()
    {
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", si.SchoolYear);
        ht.Add("Term", si.SchoolTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        ITeacherEvalHistoryLogic eduUserLogic = Container<ITeacherEvalHistoryLogic>.Instance;
        IList<TeacherEvalHistory> stuhis = eduUserLogic.FindAll(ht);
        if (stuhis.Count > 0)
            return;
        int lastYear = si.SchoolYear;
        int lastTerm = si.SchoolTerm;
        GetLastTerm(ref lastYear, ref lastTerm);
        //去年的历史
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<TeacherEvalHistory> lasthistory = Container<ITeacherEvalHistoryLogic>.Instance.FindAll(ht);



        foreach (TeacherEvalHistory mh in lasthistory)
        {
            IList<TeacherEvalType> lasttype = null;
            IList<TeacherEvalItem> lastitems = null;
            //填历史
            TeacherEvalHistory seh = new TeacherEvalHistory();
            seh.Attribute = mh.Attribute;
            seh.Caption = mh.Caption;
            seh.EvalType = mh.EvalType;
            seh.KeyWord = mh.KeyWord;
            seh.Site = mh.Site;
            seh.ManagerID = 0;
            seh.SchoolYear = si.SchoolYear;
            seh.Term = si.SchoolTerm;
            seh.Status = 0;
            seh.SchoolMark = si.SchoolMark;
            int newhistoryid = Container<ITeacherEvalHistoryLogic>.Instance.Create(seh);

            if (lasttype == null)
            {
                ht = new Hashtable();
                ht.Add("HistoryID", mh.RID);
                ht.Add("SchoolMark", si.SchoolMark);
                lasttype = Container<ITeacherEvalTypeLogic>.Instance.FindAll(ht);
            }
            if (lastitems == null)
            {
                ht = new Hashtable();
                ht.Add("HistoryID", mh.RID);
                ht.Add("SchoolMark", si.SchoolMark);
                lastitems = Container<ITeacherEvalItemLogic>.Instance.FindAll(ht);
            }
            foreach (TeacherEvalType t in lasttype)
            {
                int oldtypeid = t.RID;
                t.HistoryID = newhistoryid;
                t.SchoolMark = si.SchoolMark;
                //填类别
                int newtypeid = Container<ITeacherEvalTypeLogic>.Instance.Create(t);
                foreach (TeacherEvalItem i in lastitems)
                {
                    if (oldtypeid == i.TypeID)
                    {
                        i.TypeID = newtypeid;
                        i.HistoryID = newhistoryid;
                        i.SchoolMark = si.SchoolMark;
                        //填题目
                        Container<ITeacherEvalItemLogic>.Instance.Create(i);
                    }
                }

            }
        }

        
    }

    public static int GetLastYearID(int causeid, IList<MoralityHistory> lasthistory)
    {
        if (lasthistory.Count > 0)
        {
            return lasthistory[0].RID;
        }
        return 0;
    }
    /// <summary>
    /// 判断登录人是否具有某个身份
    /// </summary>
    /// <param name="roleid"></param>
    /// <returns></returns>
    public static bool CheckRole(int roleid)
    {
        try
        {
            IList<TeacherRoles> roles = HttpContext.Current.Session["UserRoles"] as IList<TeacherRoles>;
            foreach(TeacherRoles tr in roles)
            {
                if (tr.RoleID == roleid)
                {
                    return true;
                }
            }
        }
        catch { 

        }
        return false;
    }

    /// <summary>
    /// 判断登录人是否具有某个身份
    /// </summary>
    /// <param name="roleid"></param>
    /// <returns></returns>
    public static bool CheckRole(int roleid, int year, int term)
    {
        try
        {
            IList<TeacherRoles> roles = HttpContext.Current.Session["UserHistoryPositionRoles"] as IList<TeacherRoles>;
            foreach (TeacherRoles tr in roles)
            {
                if (tr.RoleID == roleid && year == tr.SchoolYear && term == tr.Term)
                {
                    return true;
                }
            }
        }
        catch
        {

        }
        return false;
    }
}
